package com.wyw.function.learning.demo4;

import com.wyw.function.learning.common.Dish;
import com.wyw.function.learning.common.DishClient;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Title
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * Update History:
 * Author        Time            Content
 */
public class DemoClient8 {
    // java 8 之前的方法
//    public static void main(String[] args) {
//
//        List<Dish> menu = DishClient.getMenu();
//        List<Dish> lowCaloricDishes = new ArrayList<>();
//
//        // 筛选元素
//        for (Dish dish : menu) {
//            if (dish.getCalories() < 400) {
//                lowCaloricDishes.add(dish);
//            }
//        }
//
//        // 排序
//        Collections.sort(lowCaloricDishes, new Comparator<Dish>() {
//            @Override
//            public int compare(Dish o1, Dish o2) {
//                return Integer.compare(o1.getCalories(), o2.getCalories());
//            }
//        });
//
//        // 取名字
//        List<String> lowCaloricDishNames = new ArrayList<>();
//        for (Dish dish: lowCaloricDishes) {
//            lowCaloricDishNames.add(dish.getName());
//        }
//
//        System.out.println(lowCaloricDishNames);// [seasin fruit, rice]
//    }

    // java 8 stream 流
//    public static void main(String[] args) {
//
//        List<Dish> menu = DishClient.getMenu();
//
//        List<String> collect = menu.stream()
//                .filter(dish -> dish.getCalories() < 400)
//                .sorted(Comparator.comparing(Dish::getCalories))
//                .map(Dish::getName)
//                .collect(Collectors.toList());
//
//        System.out.println(collect);// [seasin fruit, rice]
//    }

    // java 8 stream 并行流
//    public static void main(String[] args) {
//
//        List<Dish> menu = DishClient.getMenu();
//
//        List<String> collect = menu.parallelStream()
//                .filter(dish -> dish.getCalories() < 400)
//                .sorted(Comparator.comparing(Dish::getCalories))
//                .map(Dish::getName)
//                .collect(Collectors.toList());
//
//        System.out.println(collect);// [seasin fruit, rice]
//    }

    // 显示内部迭代
    public static void main(String[] args) {

        List<Dish> menu = DishClient.getMenu();

        List<String> collect = menu.parallelStream()
                .filter(dish -> {
                    System.out.println("filter: " + dish.getName());
                    return dish.getCalories() < 400;
                })
                .sorted(Comparator.comparing(Dish::getCalories))
                .map(dish -> {
                    System.out.println("mapping: " + dish.getName());
                    return dish.getName();
                })
                .collect(Collectors.toList());

        System.out.println(collect);// [seasin fruit, rice]
    }
}
